home *** CD-ROM | disk | FTP | other *** search
- /*
- * A potpourri of handy little system utilities.
- *
- * Michael Hawley
- * MIT Media Laboratory
- * 20 Ames St
- * Cambridge, MA 02139
- * mike@media-lab.mit.edu
- * Copyright (c) MIT Media Laboratory 1991
- */
- #include "util.h"
- #include <sys/stat.h>
-
- extern free(), access(), mkdir(), system(),
- strlen(), strcpy(), strcmp(), strncmp();
-
- extern char *index(), *rindex();
-
- char *av0, *_arg, *_argp; /* use by 'for_each_argument */
-
- void
- error(a,b,c,d,e,f) int a,b,c,d,e,f; { /* printf an error msg */
- printf((char *)a,b,c,d,e,f); printf("\n");
- }
-
- int Verbose=0;
-
- void
- debug(a,b,c,d) int a,b,c,d; { /* printf an error msg */
- if (Verbose) printf((char *)a,b,c,d), printf("\n");
- }
-
- int
- System(fmt, a,b,c,d,e,f,g)
- char *fmt,*a,*b,*c,*d,*e,*f,*g;
- /*
- * "printf" a system call, gripe if it failed.
- */
- {
- char t[2048];
- int i=0;
- sprintf(t,fmt,a,b,c,d,e,f,g);
- debug("!%s",t);
- if (i = system(t)) error("%s: failed! %s",av0,t);
- return !i;
- }
-
- char *
- save(s) char *s; { /* save a copy of 's' and return the pointer */
- char *t = (char *)malloc(strlen(s)+1);
- if (t) strcpy(t,s);
- return t;
- }
-
- int
- blank(s) char *s; { /* true if 's' is blank */
- while (*s == ' ' || *s=='\t' || *s == '\n' || *s == '\r' || *s == 27) ++s;
- return (int)!*s;
- }
-
- void
- stripcomment(s) char *s; { /* truncate 's' at a comment character ('#') */
- char *p = index(s,'#');
- if (p && (p==s || p[-1] != '\\')) *p = '\0';
- }
-
- #ifdef 0
- void
- stripnl(s) char *s; { /* remove trailing \n and space from 's' */
- char *p = s + strlen(s)-1;
- while (p>s && (*p=='\n' || *p == ' ' || *p=='\r')) *p-- = '\0';
- }
- #endif
-
- char *
- skipsp(s) char *s; {
- while (*s==' ' || *s=='\t' || *s == '\n') ++s;
- return s;
- }
-
- void
- squishblank(s) char *s; {
- char *t=s, *start=s;
- while (*t = *s){
- if (t>start && (*t==' ' || *t=='\t') && (t[-1]==' '||t[-1]=='\t'))
- ++s;
- else
- ++t, ++s;
- }
- }
-
- void
- squishwhite(s) char *s; {
- char *t=s, *start=s;
- while (*t = *s){
- if (t>start && (*t==' ' || *t=='\t' || *t=='\n') &&
- (t[-1]==' '||t[-1]=='\t'||t[-1]=='\n'))
- ++s;
- else
- ++t, ++s;
- }
- }
-
- char *
- prefix(s,t) char *s, *t; { /* true if 't' is a prefix of 's' */
- int sl = strlen(s);
- t = skipsp(t);
- return (*t == *s && strncmp(s,t,sl)==0)? skipsp(t+sl) : (char *)0;
- }
-
- int
- suffix(s,t) char *s, *t; { /* true if 't' is a suffix of 's' */
- s = rindex(s,'.');
- return s? strcmp(s,t)==0 : 0;
- }
-
- strtolower(s) char *s; {
- while (*s){
- if (isupper(*s)) *s = tolower(*s);
- s++;
- }
- }
-
- char *
- strindex(s,t) char *s, *t; { /* return ptr to first match of 't' in 's' */
- int n = strlen(t);
-
- if (s)
- while (*s)
- if (!strncmp(s, t, n))
- return s;
- else
- s++;
- return (char *)0;
- }
-
- char *
- stripnondigit(s) char *s; {
- char *start = s, *p = s;
-
- while (*p){
- if (isdigit(*p)) *s++ = *p;
- p++;
- }
- *s = '\0';
- return start;
- }
-
- char *
- strippunct(s) char *s; {
- char *start = s, *p = s;
-
- while (*p){
- if (ispunct(*p)) ;
- else *s = *p;
- s++, p++;
- }
- return start;
- }
-
- void
- sub(s,a,b) char *s,a,b; { /* in 's', s/a/b/g */
- while (*s){
- if (*s==a) *s=b;
- s++;
- }
- }
-
- void
- substr(s,a,b) char *s, *a, *b; { /* like 'sub', but with strings */
- char q[8192];
- char *p = s;
- int n = strlen(a);
- for (;*p;p++){
- if (*p == *a && strncmp(p,a,n)==0){
- strcpy(q,p+n);
- strcpy(p,b);
- strcpy(p+strlen(b),q);
- p += strlen(b)-1;
- }
- }
- }
-
- void
- stot(s,t,c) char *s, **t, c; { /* split 's' into a table 't' */
- char *p;
- while (p = index(s,c)){
- *p++ = '\0';
- *t++ = s;
- s = p;
- }
- *t++ = s;
- *t = (char *)0;
- }
-
- #include <sys/stat.h>
-
- int fMode(f) char *f; { /* return mode bits */
- struct stat b;
- return stat(f,&b)? 0 : b.st_mode;
- }
-
- int fSize(f) char *f; { /* return mode bits */
- struct stat b;
- return stat(f,&b)? 0 : b.st_size;
- }
-
- int fTime(f) char *f; { /* mod time of file 'f' */
- struct stat b;
- return stat(f,&b)? 0 : b.st_mtime;
- }
-
- int fDirectory(f) char *f; { /* true if file 'f' is a directory */
- struct stat b;
- return stat(f,&b)? 0 : ((b.st_mode & S_IFDIR) == S_IFDIR);
- }
-
- int fLink(f) char *f; { /* true if file 'f' is a symbolic link */
- struct stat b;
- return lstat(f,&b)? 0 : ((b.st_mode & S_IFLNK) == S_IFLNK);
- }
-
- int
- mkdirs(s,mode)
- char *s;
- int mode;
- { /* ensure that all the directories in 's' exist */
- char t[4096], *p = t;
-
- strcpy(t,s);
- if (*p=='/') ++p;
- while (p = index(p,'/')) {
- *p = '\0';
- if (access(t,0)){
- if (mkdir(t,mode))
- return 0;
- /* fixmod(t); */
- }
- *p++ = '/';
- }
- if (access(t,0) && mkdir(t,mode)) return 0;
- /* fixmod(t); */
- debug("mkdir %s",t);
- return 1;
- }
-
- #ifdef ultrix
-
- /* Ultrix version */
-
- char *re_comp();
-
- match(s,expr)
- char *s, *expr;
- /*
- * true if 's' matches regular expression 'expr'
- * (a simple regex, grep-style, *not* egrep)
- */
- {
- static char p[256]="", *q=(char *)0;
- static struct regex *r = (struct regex *)0;
- if (q != expr){
- if (strcmp(p,expr)){
- if (r) free(r);
- if (re_comp(expr))
- ;
- else return 0;
- strcpy(p,expr);
- }
- q = expr;
- }
- return re_exec(s,r)==1? 1: 0;
- }
- #else
-
- /* NeXT version */
-
- #include <regex.h>
-
- int
- match(s,expr)
- char *s, *expr;
- /*
- * true if 's' matches regular expression 'expr'
- * (a simple regex, grep-style, *not* egrep)
- */
- {
- static char p[256]="", *q=(char *)0;
- static struct regex *r = (struct regex *)0;
- if (q != expr){
- if (strcmp(p,expr)){
- if (r) free(r);
- if (r=re_compile(expr,0))
- ;
- else return 0;
- strcpy(p,expr);
- }
- q = expr;
- }
- return r? re_match(s,r)==1 : 0;
- }
- #endif
-